//
//  ChangeCareerVC.m
//  Cunli
//
//  Created by lilubin on 15/6/30.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "ChangeCareerVC.h"
#import "MyDataService.h"
#import "NSString+MD5.h"
#import "Config.h"
#import "User.h"

#define kUIScreenWidth [UIScreen mainScreen].bounds.size.width
#define kUIScreenHeight [UIScreen mainScreen].bounds.size.height


@interface ChangeCareerVC () <UIPickerViewDelegate, UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UITextField *careerField;

@property (nonatomic, strong) UIPickerView *careerPickerView; //选择职业
@property (nonatomic, strong) NSArray *careerArray; //存储返回的“list”中的数据
@property (nonatomic, strong) NSString *userCareer; //career的id

@end

@implementation ChangeCareerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setAllUIKitAttributes];
    [self reloadCareerData];
    [self initCareerPickerView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)reloadCareerData
{
    NSMutableDictionary *careerParams = [NSMutableDictionary dictionaryWithCapacity:2];
    [careerParams setObject:[Config getOwnUserID] forKey:@"userid"];
    NSString *sign = [NSString getSignFromParam:careerParams andSession:[Config getOwnSession]];
    [careerParams setObject:sign forKey:@"sign"];
    
    [MyDataService requestURL:@"career.php" httpMethod:@"GET" params:careerParams fileDatas:nil complection:^(id result)
    {
        if ([result[@"response"] isEqualToString:@"career"])
        {
            self.careerArray = [result objectForKey:@"list"];
        }
        else
        {
            [Config showHUDWithText:@"无法获取职业信息" andImage:@"error"];

        }
        
    } failureAction:^{
        [Config showHUDWithText:@"网络错误，无法获取职业信息" andImage:@"error"];
    }];
}

- (void)initCareerPickerView
{
    
    //初始化选择职业的pickerView
    _careerPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(20, kUIScreenHeight - 200, kUIScreenWidth-40, 280)];
    _careerPickerView.delegate = self;
    _careerPickerView.dataSource = self;
    _careerPickerView.backgroundColor = [UIColor clearColor];
    
    _careerField.inputView = _careerPickerView;
}

- (void)setAllUIKitAttributes
{
    self.navigationItem.title = @"职业";
    
    
//    if ([CLUser shareCLUser].career != nil) {
//        _careerField.text = [CLUser shareCLUser].career;
//    }
    
    if ([Login curLoginDetailUser].career != nil) {
        _careerField.text = [Login curLoginDetailUser].career;
    }
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 65, 30);
//    [rightBtn setBackgroundImage:[UIImage imageNamed:@"regnext"] forState:UIControlStateNormal];
    [rightBtn setTitle:@"确认" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(commitAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    flexSpacer.width = flexSpacer.width - 20;
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:flexSpacer, rightBarItem, nil]];
    
}

- (void)commitAction:(UIButton *)button
{
    [self.view endEditing:YES];
    
    if (_userCareer.length != 0) {
        NSMutableDictionary *CheckParams = [NSMutableDictionary dictionaryWithCapacity:4];
        [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
        [CheckParams setObject:_userCareer forKey:@"value"];
        [CheckParams setObject:@"career" forKey:@"field"];
        
        NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
        [CheckParams setObject:resultSign forKey:@"sign"];
        
        [MyDataService requestURL:@"edit_profile.php" httpMethod:@"POST" params:CheckParams fileDatas:nil complection:^(id result) {
            if ([result[@"response"] isEqualToString:@"edit_profile"]) {
//                [CLUser shareCLUser].career = _careerField.text;
                [Login curLoginDetailUser].career = _careerField.text;
                [self.navigationController popViewControllerAnimated:YES];
            }
            else if([result[@"response"] isEqualToString:@"error"])
            {
                NSDictionary *dic = result[@"error"];
                [Config showHUDWithText:dic[@"text"] andImage:@"error"];
            }
        } failureAction:^{
            [Config showHUDWithText:@"职业修改失败" andImage:@"error"];
        }];
    }
    
}


#pragma -mark dataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0)
    {
        return self.careerArray.count;
    }
    
    if (component == 1) {
        NSInteger selCareer = [pickerView selectedRowInComponent:0];
        
        NSDictionary *selectedCareerDic = self.careerArray[selCareer];
        
        NSArray *childCareer = [selectedCareerDic objectForKey:@"child"];
        
        return childCareer.count;
    }
    return 1;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    if (component == 0) {
        NSDictionary *careerDic = self.careerArray[row];
        NSString *parentName = [careerDic objectForKey:@"name"];
        [pickerView selectRow:0 inComponent:1 animated:YES];
        return parentName;
    }
    else
    {
        NSInteger selCareer = [pickerView selectedRowInComponent:0];
        NSDictionary *careerDic2 = self.careerArray[selCareer];
        if (careerDic2.count != 0) {
            NSArray *childCareerArray = [careerDic2 objectForKey:@"child"];
            NSInteger selRow = row;
            if (row > childCareerArray.count-1) {
                selRow = childCareerArray.count-1;
            }
            NSString *childCareer = [childCareerArray[selRow] objectForKey:@"name"];
            return childCareer;
        }
    }
    return @"";
    
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
        if (component == 0)
        {
            [pickerView selectRow:0 inComponent:1 animated:YES];
            [pickerView reloadComponent:1];
        }
        if (component == 1) {
            
            NSInteger selCareer1 = [pickerView selectedRowInComponent:0];
            
            NSDictionary *careerDic = self.careerArray[selCareer1];
            
            NSString *careerName1 = [careerDic objectForKey:@"name"];
            
            NSArray *childCareerArray = [careerDic objectForKey:@"child"];
            
            NSInteger selCareer2 = [pickerView selectedRowInComponent:1];
            
            if (childCareerArray.count != 0) {
                
                NSDictionary *childCareerDic = childCareerArray[selCareer2];
                
                NSString *careerName2 = [childCareerDic objectForKey:@"name"];
                
                
                NSString *careerName = [NSString stringWithFormat:@"%@ %@", careerName1, careerName2];
                
                _careerField.text = careerName;
                _userCareer = [childCareerDic objectForKey:@"id"];
                
            }
            else
            {
                _careerField.text = careerName1;
                _userCareer = careerDic[@"id"];
            }
        }
}






@end
